//=============================================================================
//
//	Dingenskirchen (Beta 1) - dingenskirchen_b1.gm
//
//	Who 	        When		        What
//-----------------------------------------------------------------------------
//  Mateos          27 July 2014        Merge of d00d/palota and Loffy scripts
//  palota          11 March 2014       Last Assembla commit (cappoints)
//  d00d            1st March 2014      What I have so far
//	^1Loffy         26 February 2014	Initial Script
//  KeMoN           25 February 2014    Initial submission on OB forums
//
//=============================================================================

/*
 * TODO List
 * - Find out why ESCORT_Tank get a vehicle path failed while it seems reachable...
 * - Move the BUILD_Tank at the bottom
 * - Add routing for initial BUILD_Tank and CHECKPOINT_village_flag
 * - Add ATTACK goals for Allies
 * - Cover Spot for PLANT_Tank_Barrier
 * - Test
 * - Continue after the Forward Gate to the end
 */

global Map =
{
	Debug = 1,  // Please set to zero before distributing your script
	ShowMovers = false,

	Allied_Command_Post_Dyno = 0,
	Axis_Command_Post_Dyno = 0,
	Bridge_Dyno = 0,
	Conference_Room_Dyno = 0,
	Forward_Gate_Dyno = 0,
	Tank_Barrier_Dyno = 0,

    CastleGateDestroyed = 0,

    // *** TRIGGER CALLBACKS ***
	Allied_Command_Post_Built = function( trigger )
	{
		if ( TestMap )
			{ return; }
		Util.MapDebugPrint( "Allied_Command_Post_Built" );
	},

	Axis_Command_Post_Built = function( trigger )
	{
		if ( TestMap )
			{ return; }
		Util.MapDebugPrint( "Axis_Command_Post_Built" );
	},

	Bridge_Built = function( trigger )
	{
		if ( TestMap )
			{ return; }

		SetAvailableMapGoals( TEAM.ALLIES, false, "BUILD_Bridge" );

		Util.MapDebugPrint( "Bridge_Built" );
	},

	Tank_Built = function( trigger )
	{
		if ( TestMap )
			{ return; }

		if ( !Map.CastleGateDestroyed )
        {
            SetAvailableMapGoals( TEAM.ALLIES, true, "ESCORT_Tank" );
            SetAvailableMapGoals( TEAM.ALLIES, true, "MOUNT_Tank" );
        }

		Util.MapDebugPrint( "Tank_Built" );
	},

	Tank_Barrier_Built = function( trigger )
	{
		if ( TestMap )
			{ return; }

        SetAvailableMapGoals( TEAM.ALLIES, true, "PLANT_Tank_Barrier" );

		Util.MapDebugPrint( "Tank_Barrier_Built" );
	},

	Allied_Command_Post_Planted = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Allied_Command_Post_Dyno += 1;

		SetAvailableMapGoals( TEAM.AXIS, false, "PLANT_Allied_Command_Post" );

		Util.MapDebugPrint( "Allied_Command_Post_Planted" );
	},

	Axis_Command_Post_Planted = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Axis_Command_Post_Dyno += 1;

		SetAvailableMapGoals( TEAM.ALLIES, false, "PLANT_Axis_Command_Post" );

		Util.MapDebugPrint( "Axis_Command_Post_Planted" );
	},

	Bridge_Planted = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Bridge_Dyno += 1;

		SetAvailableMapGoals( TEAM.AXIS, false, "PLANT_Bridge" );

		Util.MapDebugPrint( "Bridge_Planted" );
	},

	Conference_Room_Planted = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Conference_Room_Dyno += 1;

		SetAvailableMapGoals( TEAM.ALLIES, false, "PLANT_Conference_Room" );

		Util.MapDebugPrint( "Conference_Room_Planted" );
	},

	Forward_Gate_Planted = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Forward_Gate_Dyno += 1;

		SetAvailableMapGoals( TEAM.ALLIES, false, "PLANT_Forward_Gate" );

		Util.MapDebugPrint( "Forward_Gate_Planted" );
	},

	Tank_Barrier_Planted = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Tank_Barrier_Dyno += 1;

		SetAvailableMapGoals( TEAM.ALLIES, false, "PLANT_Tank_Barrier" );

		Util.MapDebugPrint( "Tank_Barrier_Planted" );
	},

	Allied_Command_Post_Defused = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Allied_Command_Post_Dyno -= 1;

		if ( Map.Allied_Command_Post_Dyno < 1 )
			{ SetAvailableMapGoals( TEAM.AXIS, true, "PLANT_Allied_Command_Post" ); }

		Util.MapDebugPrint( "Allied_Command_Post_Defused" );
	},

	Axis_Command_Post_Defused = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Axis_Command_Post_Dyno -= 1;

		if ( Map.Axis_Command_Post_Dyno < 1 )
			{ SetAvailableMapGoals( TEAM.ALLIES, true, "PLANT_Axis_Command_Post" ); }

		Util.MapDebugPrint( "Axis_Command_Post_Defused" );
	},

	Bridge_Defused = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Bridge_Dyno -= 1;

		if ( Map.Bridge_Dyno < 1 )
			{ SetAvailableMapGoals( TEAM.AXIS, true, "PLANT_Bridge" ); }

		Util.MapDebugPrint( "Bridge_Defused" );
	},

	Conference_Room_Defused = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Conference_Room_Dyno -= 1;

		if ( Map.Conference_Room_Dyno < 1 )
			{ SetAvailableMapGoals( TEAM.ALLIES, true, "PLANT_Conference_Room" ); }

		Util.MapDebugPrint( "Conference_Room_Defused" );
	},

	Forward_Gate_Defused = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Forward_Gate_Dyno -= 1;

		if ( Map.Forward_Gate_Dyno < 1 )
			{ SetAvailableMapGoals( TEAM.ALLIES, true, "PLANT_Forward_Gate" ); }

		Util.MapDebugPrint( "Forward_Gate_Defused" );
	},

	Tank_Barrier_Defused = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Tank_Barrier_Dyno -= 1;

		if ( Map.Tank_Barrier_Dyno < 1 )
			{ SetAvailableMapGoals( TEAM.ALLIES, true, "PLANT_Tank_Barrier" ); }

		Util.MapDebugPrint( "Tank_Barrier_Defused" );
	},

	Allied_Command_Post_Destroyed = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Allied_Command_Post_Dyno = 0;

		Util.MapDebugPrint( "Allied_Command_Post_Destroyed" );
	},

	Axis_Command_Post_Destroyed = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Axis_Command_Post_Dyno = 0;

		Util.MapDebugPrint( "Axis_Command_Post_Destroyed" );
	},

	Bridge_Destroyed = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Bridge_Dyno = 0;

		SetAvailableMapGoals( TEAM.ALLIES, true, "BUILD_Bridge" );

		Util.MapDebugPrint( "Bridge_Destroyed" );
	},

	Conference_Room_Destroyed = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Conference_Room_Dyno = 0;

		Util.MapDebugPrint( "Conference_Room_Destroyed" );
	},

	Forward_Gate_Destroyed = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Forward_Gate_Dyno = 0;

		SetAvailableMapGoals( TEAM.ALLIES, false, "PLANT_Forward_Gate" );

		Util.MapDebugPrint( "Forward_Gate_Destroyed" );
	},

	Tank_Barrier_Destroyed = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.CastleGateDestroyed = 1;
        Map.Tank_Barrier_Dyno = 0;

		Util.MapDebugPrint( "Tank_Barrier_Destroyed" );
	},

	castle_gate_Destroyed = function( trigger )
	{
		sleep(2);

		Util.EnableGoal("FLAG_.*");

		SetAvailableMapGoals( TEAM.ALLIES, false, "ESCORT_Tank" );
        SetAvailableMapGoals( TEAM.ALLIES, false, "MOUNT_Tank" );

		Util.MapDebugPrint( "castle_gate_Destroyed" );
	},

	GetBotWithFlag = function(goal)
	{
		ent = GetGoal(goal).GetEntity();
		owner = GetEntOwner(ent);
		if (EntityIsValid(owner))
        {
			Util.MapDebugPrint(goal + " taken by " + GetEntName(owner));
			return Util.IsBot(owner);
		}

		//function GetEntOwner sometimes does not work
		//enable both cappoints
		foreach(bot in BotTable){
			if(bot.IsCarryingFlag()){
				Util.MapDebugPrint("Enabled both cappoints for bot " + bot.Name);
				bot.SetRoles(ROLE.DEFENDER2, ROLE.DEFENDER3);
			}
		}
		return null;
	},

	gold_Taken = function( trigger )
	{
		if ( TestMap )
			{ return; }

		bot = Map.GetBotWithFlag("FLAG_gold");
		if (bot)
        {
			bot.ClearRoles(ROLE.DEFENDER2);
			bot.SetRoles(ROLE.DEFENDER3);
		}

		Util.MapDebugPrint( "gold_Taken" );
	},

	gold_Returned = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Util.MapDebugPrint( "gold_Returned" );
	},

	gold_Secured = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Util.DisableGoal("FLAG_gold");
		Util.DisableGoal("CAPPOINT_treasure");
		Util.MapDebugPrint( "gold_Secured" );
	},

	microfilm_Taken = function( trigger )
	{
		if ( TestMap )
			{ return; }

		bot = Map.GetBotWithFlag("FLAG_microfilm");
		if ( bot )
        {
			bot.ClearRoles(ROLE.DEFENDER3);
			bot.SetRoles(ROLE.DEFENDER2);
		}

		Util.MapDebugPrint( "microfilm_Taken" );
	},

	microfilm_Returned = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Util.MapDebugPrint( "microfilm_Returned" );
	},

	microfilm_Secured = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Util.DisableGoal( "FLAG_microfilm" );
		Util.DisableGoal( "CAPPOINT_microfilm" );

		Util.MapDebugPrint( "microfilm_Secured" );
	},

	river_flag_Axis_Captured = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Util.MapDebugPrint( "river_flag_Axis_Captured" );
	},

	river_flag_Allies_Captured = function( trigger )
	{
		if ( TestMap )
			{ return; }

        SetAvailableMapGoals( TEAM.ALLIES, true, "BUILD_Bridge" );

		Util.MapDebugPrint( "river_flag_Allies_Captured" );
	},

	village_flag_Axis_Captured = function( trigger )
	{
		if ( TestMap )
			{ return; }

        SetAvailableMapGoals( TEAM.ALLIES, false, "PLANT_Forward_Gate" );

		Util.MapDebugPrint( "village_flag_Axis_Captured" );
	},

	village_flag_Allies_Captured = function( trigger )
	{
		if ( TestMap )
			{ return; }

        SetAvailableMapGoals( TEAM.ALLIES, true, "PLANT_Forward_Gate" );

		Util.MapDebugPrint( "village_flag_Allies_Captured" );
	},

    // ************************************************************************

    Tank_Stolen = function( trigger )
	{
		if ( TestMap )
			{ return; }

        SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_Bravo.*" );
        SetAvailableMapGoals( TEAM.AXIS, false, "DEFEND_Alpha.*" );

        SetAvailableMapGoals( TEAM.ALLIES, true, "BUILD_Allied_Command_Post" );
        SetAvailableMapGoals( TEAM.ALLIES, true, "CHECKPOINT_village_flag" );

		Util.MapDebugPrint( "Tank_Stolen" );
	},

    Tank_Passed_First_Barrier = function( trigger )
    {
		if ( TestMap )
			{ return; }

        Util.MapDebugPrint( "Tank_Passed_First_Barrier" );
    },

    Village_Secured = function( trigger )
    {
		if ( TestMap )
			{ return; }

        Util.DisableGoal( "CHECKPOINT_village_flag" );

        SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_Charlie.*" );
        SetAvailableMapGoals( TEAM.AXIS, true, "MOUNTMG42_forward_gate_mg" );
        SetAvailableMapGoals( TEAM.AXIS, false, "DEFEND_Bravo.*" );

		Util.MapDebugPrint( "Village_Secured" );
    },

    // ************************************************************************

    vehicle_at_location = function( trigger )
    {
        switch(Map.VehicleLocation)
        {
            case 1:
            {
                Map.Tank_Stolen();
            }
        }

        //Util.MapDebugPrint("vehicle at location " + Map.VehicleLocation, true);
    },
};

global OnMapLoad = function()
{
	if ( TestMapOn )
		{ Util.AutoTestMap(); }

    // *** TRIGGERS ***
	OnTrigger( "^5Allied Command Post constructed. Charge speed increased!", Map.Allied_Command_Post_Built );
	OnTrigger( "^1Axis team has destroyed the Allied Command Post!", Map.Allied_Command_Post_Destroyed );
	OnTrigger( "^1Axis Command Post constructed. Charge speed increased!", Map.Axis_Command_Post_Built );
	OnTrigger( "^5Allied team has destroyed the Axis Command Post!", Map.Axis_Command_Post_Destroyed );
	OnTrigger( "^5The Allies have constructed the Bridge!", Map.Bridge_Built );
	OnTrigger( "^5The Tank has been repaired!", Map.Tank_Built );
	OnTrigger( "^1The Axis have constructed the Tank Barrier!", Map.Tank_Barrier_Built );

	OnTrigger( "Planted at the Allied Command Post.", Map.Allied_Command_Post_Planted );
	OnTrigger( "Planted at the Axis Command Post.", Map.Axis_Command_Post_Planted );
	OnTrigger( "Planted at the Bridge.", Map.Bridge_Planted );
	OnTrigger( "Planted at the Conference Room.", Map.Conference_Room_Planted );
	OnTrigger( "Planted at the Forward Gate.", Map.Forward_Gate_Planted );
	OnTrigger( "Planted at the Tank Barrier.", Map.Tank_Barrier_Planted );

	OnTrigger( "Defused at the Allied Command Post.", Map.Allied_Command_Post_Defused );
	OnTrigger( "Defused at the Axis Command Post.", Map.Axis_Command_Post_Defused );
	OnTrigger( "Defused at the Bridge.", Map.Bridge_Defused );
	OnTrigger( "Defused at the Conference Room.", Map.Conference_Room_Defused );
	OnTrigger( "Defused at the Forward Gate.", Map.Forward_Gate_Defused );
	OnTrigger( "Defused at the Tank Barrier.", Map.Tank_Barrier_Defused );

	OnTrigger( "^1The Axis have destroyed the Bridge!", Map.Bridge_Destroyed );
	OnTrigger( "^5The Allies have destroyed the Conference Room!", Map.Conference_Room_Destroyed );
	OnTrigger( "^5The Allies have destroyed the Forward Gate!", Map.Forward_Gate_Destroyed );
	OnTrigger( "^5The Allies have destroyed the Tank Barrier!", Map.Tank_Barrier_Destroyed );

	OnTrigger( "Allies have stolen the Treasure!", Map.gold_Taken );
	OnTrigger( "^1The Axis have returned the Treasure!", Map.gold_Returned );
	OnTrigger( "^5The Allies have secured the Treasure!", Map.gold_Secured );
	OnTrigger( "Allies have stolen the Microfilm!", Map.microfilm_Taken );
	OnTrigger( "^1The Axis have returned the Microfilm!", Map.microfilm_Returned );
	OnTrigger( "^5The Allies have transmitted the Microfilm!", Map.microfilm_Secured );

	OnTrigger( "^1The Axis have reclaimed the River Depot!", Map.river_flag_Axis_Captured );
	OnTrigger( "^5The Allies have captured the River Depot!", Map.river_flag_Allies_Captured );
	OnTrigger( "^1The Axis have reclaimed the Village!", Map.village_flag_Axis_Captured );
	OnTrigger( "^5The Allies have captured the Village!", Map.village_flag_Allies_Captured );
	OnTrigger( "^5The Allies have destroyed the Castle Gate!", Map.castle_gate_Destroyed );

    // In order to follow Tank progress, some more triggers
    OnTrigger( "^5The Tank has passed Tank Barrier #1!", Map.Tank_Passed_First_Barrier );
    OnTrigger( "^5The Allies have secured the Village!", Map.Village_Secured );

    // *** VEHICLE TRIGGERS ***
    OnTrigger( "tank at location", Map.vehicle_at_location );

    Trigger1 = OnTriggerRegion(AABB(1772.368,3937.222,-382.747,1914.477,4111.901,-301.914), RegionTrigger.VehicleTrigger);

	/* More triggers:

		^5The Tank has passed Tank Barrier #1!
		^5The Tank has passed Tank Barrier #2!

		^5The Allies have destroyed the Castle Gate!

		^5Shock Treatment activated!
		^1Shock Treatment deactivated!
		^5The Allies have barbecued the Dead Prisoner!

		^5The Allies have secured the Village!
		^5The Allies have secured the River Depot!
		^5The Allies can now capture the River Depot!

		^5The Allies have destroyed the Side Wall!
		^5The Allies have destroyed the Sewers Gate!
		^5The Allies have destroyed the Bergfried Wall!

		^5The Allies have won the Match!
		Allied Mission Status: ^20/2 ^7Objectives completed!
		Allied Mission Status: ^21/2 ^7Objectives completed!
		Allied Mission Status: ^22/2 ^7Objectives completed!

	*/

    // *** CLEAR ALL GOALS FOR BOTH TEAMS ***
    Util.DisableGoal( ".*", true ); // All but Routes

    Util.EnableGoal( "AMMOCAB_village_cabinet_ammo_model" );
    Util.EnableGoal( "HEALTHCAB_village_cabinet_health_model" );

    // *** AXIS GOALS ***
    SetAvailableMapGoals( TEAM.AXIS, true, "BUILD_Tank_Barrier" );
    SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_Alpha.*" );
    SetAvailableMapGoals( TEAM.AXIS, true, "MOUNTMG42_village_mg" );

    // *** ALLIED GOALS ***
    // SetAvailableMapGoals(TEAM.ALLIES, true, "AMMOCAB_parachute_ammo");
    SetAvailableMapGoals( TEAM.ALLIES, true, "BUILD_Tank" );
    SetAvailableMapGoals( TEAM.ALLIES, true, "MOVER_tank" );

    // *** ROLES ***
	Util.SetRoleForGoals( "CAPPOINT_microfilm",  ROLE.DEFENDER2 );
	Util.SetRoleForGoals( "CAPPOINT_treasure",   ROLE.DEFENDER3 );

	// Max users per goal
	Util.SetMaxUsers( 1, ".*" );
	Util.SetMaxUsers( 2, "HEALTHCAB_.*" );
	Util.SetMaxUsers( 2, "AMMOCAB_.*" );

	// Camp times
	SetMapGoalProperties( "MOUNTMG42_.*",   {MinCampTime = 15, MaxCampTime = 90} );
	SetMapGoalProperties( "MOBILEMG42_.*",  {MinCampTime = 10, MaxCampTime = 60} );

    // Misc
    SetGoalPriority( "BUILD_Allied_Command_Post", 0.80);   // Was 0.9 while PLANT_Tank_Barrier is 0.81
    Util.SetGoalOffset( 275, 0, 10, "PLANT_Tank_Barrier" );
    // Util.AddUsePoint( "PLANT_Tank_Barrier", {Vec3(2235, 3150, -315)} );

	Util.MapDebugPrint( "Omni-bot map script for Dingenskirchen (Beta 1) executed." );
};

global OnBotJoin = function( bot )
{
	bot.TargetBreakableDist = 90.0;
	bot.MaxViewDistance = 2600;
};

global InitializeRoutes = function()
{
	MapRoutes =
	{
		MOUNTMG42_lake_mg =
		{
		},
		BUILD_Axis_Command_Post =
		{
		},
		BUILD_Tank =
		{
		},
		BUILD_Bridge =
		{
		},
		BUILD_Allied_Command_Post =
		{
		},
		BUILD_Tank_Barrier =
		{
		},
		PLANT_Bridge =
		{
		},
		PLANT_Tank_Barrier =
		{
		},
		PLANT_Forward_Gate =
		{
		},
		PLANT_Conference_Room =
		{
		},
		PLANT_Axis_Command_Post =
		{
		},
		PLANT_Allied_Command_Post =
		{
		},
		FLAG__gold =
		{
		},
		FLAG_microfilm =
		{
		},
		FLAG_gold =
		{
		},
		CHECKPOINT_village_flag =
		{
		},
		CHECKPOINT_river_flag =
		{
		},
	};
	Util.Routes(MapRoutes);
};

